$(function () {
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , laydate = layui.laydate;
        laydate.render({
            elem: '#deadline',
            max: 1,
            value: PEMS.formateDate(new Date(), 'yyyy-MM-dd')
        });

        form.on('submit(recordSave)', function () {
            saveFormData();
        })
    })
});

/**
 * 保存数据
 */
function saveFormData() {

    if (!$("#cusName").val()) {
        PEMS.openInFoWindow("提示信息", "请输入客户名称", function () {
            layer.closeAll();
        });
        return;
    }
    //验证手机号
    var cusPhone = $("#cusPhone").val()
    if (!cusPhone) {
        PEMS.openInFoWindow("提示信息", "请输入客户手机号", function () {
            layer.closeAll();
        });
        return;
    }
    if (!/^(13|15|18|14|17)\d{9}$/i.test(cusPhone)) {
        PEMS.openInFoWindow("提示信息", "手机号码格式不正确", function () {
            layer.closeAll();
        });
        return;
    }
    var maintenanceFees = $("#maintenanceFees").val();
    if (!/((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/.test(maintenanceFees)) {
        PEMS.openInFoWindow("提示信息", "维修费用格式不正确", function () {
            layer.closeAll();
        });
        return;
    }

    if (!$("#carRecord").val()) {
        PEMS.openInFoWindow("提示信息", "请输入维修内容", function () {
            layer.closeAll();
        });
        return;
    }
    loading();
    $.ajax({
        url: WebContext + "/cardRecord/insertRecord",
        data: {
            cusName: $("#cusName").val(),
            cusPhone: $("#cusPhone").val(),
            carRecord: $("#carRecord").val(),
            createTime: $("#deadline").val(),
            carType: $("#carType").val(),
            maintenanceFees: $("#maintenanceFees").val()
        },
        type: "post",
        dataType: "json",
        success: function (data) {
            if (data.success == true) {
                PEMS.openInFoWindow("提示信息", "数据保存成功", function () {
                    loading(1);
                    getIframeWindowByContent("首页").tableRefresh("resourceTable", true, null, null);
                    closeByBtnInSubPage(getITagByContent("新增维修记录"));
                });

            }
        }
    });
}
